/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "test.h"

int test_process() {
    int status;

    Sprint("Parent PID: %d\n", getpid());

    pid_t pid = Sfork();

    if (pid == -1) {
        Sprint("Fork failed!\n");
        return 1;
    } else if (pid == 0) {
        const char *program = "/bin/ls";
        char *args[] = { "ls", NULL };

        if (Sexec(program, args) == -1) {
            Sprint("Exec failed!\n");
            return 1;
        }
    } else {
        pid_t child_pid = Swait(&status);
        if (child_pid > 0) {
            if (WIFEXITED(status)) {
                Sprint("Child terminated with status: %d\n", WEXITSTATUS(status));
            }
        }
    }
    return 0;
}
